﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace FileNameCheck
{
    public static class MainFormEvent
    {
        public static Main MainWin;

        public static void SetWin(Main m)
        {
            MainWin = m;
            InitEvent();
            InitData();
        }

        static void InitData()
        {
            MainWin.txt_Path.Text = Properties.Settings.Default.Path;
            MainWin.txt_Start.Text = Properties.Settings.Default.StartWith;
            MainWin.txt_End.Text = Properties.Settings.Default.EndWith;
            MainWin.txt_Ignore.Text = Properties.Settings.Default.Ignore;
            MainWin.ce_Save.Checked = Properties.Settings.Default.SaveLog;
            MainWin.ce_Folder.Checked = Properties.Settings.Default.CheckFolder;
            MainWin.ud_Depth.Value = Properties.Settings.Default.CheckDepth;
            MainWin.txt_Tex.Text = Properties.Settings.Default.TextureType;
            MainWin.txt_Tex_End.Text = Properties.Settings.Default.TextureEnd;
        }

        static void SaveData()
        {
            Properties.Settings.Default.Path = MainWin.txt_Path.Text;
            Properties.Settings.Default.StartWith = MainWin.txt_Start.Text;
            Properties.Settings.Default.EndWith = MainWin.txt_End.Text;
            Properties.Settings.Default.Ignore = MainWin.txt_Ignore.Text;
            Properties.Settings.Default.SaveLog = MainWin.ce_Save.Checked;
            Properties.Settings.Default.CheckFolder = MainWin.ce_Folder.Checked;
            Properties.Settings.Default.CheckDepth = Convert.ToInt32(MainWin.ud_Depth.Value);
            Properties.Settings.Default.TextureType = MainWin.txt_Tex.Text;
            Properties.Settings.Default.TextureEnd = MainWin.txt_Tex_End.Text;
            Properties.Settings.Default.Save();
        }

        static void InitEvent()
        {
            MainWin.btn_Path.Click += BtnPathClick;
            MainWin.btn_Check.Click += BtnCheckClick;
            //MainWin.btn_Check.Click += Test;
        }

        static void BtnPathClick(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "Select a folder for check name";
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                MainWin.txt_Path.Text = fbd.SelectedPath;
            }
        }

        static void BtnCheckClick(object sender, EventArgs e)
        {
            string r = MainWin.txt_Path.Text;
            string s = MainWin.txt_Start.Text;
            string es = MainWin.txt_End.Text;
            string ig = MainWin.txt_Ignore.Text;
            string texT = MainWin.txt_Tex.Text;
            string texE = MainWin.txt_Tex_End.Text;
            int d = Convert.ToInt32(MainWin.ud_Depth.Value);
            SaveData();

            FileNameCheckHelper check = new FileNameCheckHelper(r, d, s, es, ig,texT,texE);
            check.CheckFolder = MainWin.ce_Folder.Checked;
            List<string> paths = check.Check();

            MainWin.fp_Result.Controls.Clear();

            foreach (string str in paths)
            {
                LinkLabel link = new LinkLabel();
                link.Text = str;
                link.Size = new System.Drawing.Size(MainWin.fp_Result.Size.Width-10, 25);
                FileInfo fi = new FileInfo(str);                
                link.Click += (ss, ev) => { System.Diagnostics.Process.Start(fi.Directory.FullName); };
                MainWin.fp_Result.Controls.Add(link);
            }

            MessageBox.Show("Check Over!"+(paths.Count>0?"Unmatch:"+paths.Count:"All match!"));

            if(MainWin.ce_Save.Checked)
            {
                SaveLog(paths);
            }
        }

        static void SaveLog(List<string> paths)
        {
            DateTime now = DateTime.Now;
            StreamWriter swlog = new StreamWriter(now.ToString("yyyy_MM_dd-HH_mm_ssss") + ".txt");
            swlog.WriteLine($"Check on {now.ToString()}");
            swlog.WriteLine($"{paths.Count} files or folders are not match rule.\n\n");
            foreach (string str in paths)
            {
                swlog.WriteLine(str);
            }
            swlog.Close();
            swlog.Dispose();
        }

#if DEBUG
        static void Test(object sender, EventArgs e)
        {
            TextureHelper.Test();
        }
#endif
    }
}
